{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一元线性回归教程\n",
    "\n",
    "* numpy - 矩阵计算\n",
    "* scipy - 微积分之类的操作\n",
    "* pandas - 基于numpy开发的数据处理库，python上的R语言\n",
    "* matplotlib - 数据可视化\n",
    "* scikit-learn - 机器学习库\n",
    "* tensorflow/pytorch - 深度学习框架"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### —— 用最小二乘法求解一元线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从numpy导入所有\n",
    "from numpy import *\n",
    "import numpy\n",
    "# 导入画图工具，as是别名的意思\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1. 导入数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们用一组现成的数据，文件 data.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAaq0lEQVR4nO3df4xdZZ3H8feX6SAD6zoCEwJTamsk06gVihPEdGOk6BaFwKQahcguKpv+sWbXdXerbdasy0a3Nd0Nstkspos/2OBCEWrxx8ZKaI0JsZDpFq386IIgpcOP1kDZCCNMy3f/uOfS6cz9ce49v55zzueVNDP3zJ07zz3T+Z7n+T7f5znm7oiISLWcUHQDREQkfQruIiIVpOAuIlJBCu4iIhWk4C4iUkELim4AwOmnn+6LFy8uuhkiIqWye/fu37r7SKuvBRHcFy9ezOTkZNHNEBEpFTN7st3XlJY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V+MjmX6f0PBXUSkgpSWERGpIAV3EZEKUnAXEakgBXcRkQpScBcRqSAFdxGRClJwFxGpoP8H1k8f4K2JgJEAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 从data.csv文件中加载数据，以逗号为分隔符\n",
    "points = numpy.genfromtxt('data.csv', delimiter=',')\n",
    "\n",
    "# 提取 x，y\n",
    "# 冒号的意思是所有数据,0表示第0列，1表示第1列\n",
    "x = numpy.array(points[:,0])\n",
    "y = numpy.array(points[:,1])\n",
    "\n",
    "# 用plt做出散点图\n",
    "plt.scatter(x,y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
      "[2, 3]\n"
     ]
    }
   ],
   "source": [
    "arr = [1,2,3,4,5,6,7,8,9,10]\n",
    "print(arr[-1])\n",
    "print(arr[:])\n",
    "print(arr[1:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2. 定义损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义线性模型 y = mx + b\n",
    "\n",
    "def compute_cost(points, b, m):\n",
    "    # 初始化总的损失值为0\n",
    "    total_cost = 0\n",
    "    # N为训练数据的数量\n",
    "    N = len(points)\n",
    "    \n",
    "    # 计算所有误差平方的和 sum(y-mx-b)^2\n",
    "    # N个训练数据，所以循环N次，[0,N)\n",
    "    for i in range(N):\n",
    "        # 第i行，第0列\n",
    "        x = points[i, 0]\n",
    "        # 第i行，第1列\n",
    "        y = points[i, 1]\n",
    "        \n",
    "        # **为求平方\n",
    "        total_cost += (y - m * x - b) ** 2\n",
    "    \n",
    "    return total_cost / float(N)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3. 定义拟合算法函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 需要计算 x_avg\n",
    "# 计算平均值的函数\n",
    "def average(data):\n",
    "    # sum是总和\n",
    "    sum = 0\n",
    "    # num是总数据量\n",
    "    num = len(data)\n",
    "    for i in range(num):\n",
    "        sum += data[i]\n",
    "    return sum / float(num)\n",
    "\n",
    "# fit为拟合的意思\n",
    "def fit(points):\n",
    "    # N是训练数据的总数\n",
    "    N = len(points)\n",
    "    # x的平均值\n",
    "    x_bar = average(array(points[:, 0]))\n",
    "    \n",
    "    sum_yx = 0\n",
    "    sum_x2 = 0\n",
    "    \n",
    "    sum_b = 0\n",
    "    \n",
    "    # 求m\n",
    "    for i in range(N):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        sum_yx += y * (x - x_bar)\n",
    "        sum_x2 += x ** 2\n",
    "        \n",
    "    m = sum_yx / ( sum_x2 - x_bar ** 2 * float(N))\n",
    "    \n",
    "    # 求b\n",
    "    for i in range(N):\n",
    "        x = points[i, 0]\n",
    "        y = points[i, 1]\n",
    "        sum_b += y - m * x\n",
    "        \n",
    "    b = sum_b / float(N)\n",
    "    \n",
    "    return m, b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4. 测试：运行fit函数来计算最优的m和b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "m is: 1.3224310227553846\n",
      "b is: 7.991020982269173\n",
      "cost is: 110.25738346621313\n"
     ]
    }
   ],
   "source": [
    "m, b = fit(points)\n",
    "\n",
    "print(\"m is:\",m)\n",
    "print(\"b is:\",b)\n",
    "\n",
    "# 计算损失函数\n",
    "cost = compute_cost(points, b, m)\n",
    "\n",
    "print(\"cost is:\", cost)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5. 画出拟合曲线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3df3RdVZn/8ffTNEBEIZZ2IU2piWOnDFKwNRax31EoaEEqZCqDoKMMonWN4BfRqW34oi5mwJapCjgoI6KCgtAKNUCLVKZFZ1a1YEsKFSFY7Q8afrQIQbAR0nR//zj3NsnNub/PvefX57VWV3LPPffenZPmOfs8+9n7mHMOERFJljFhN0BERIKn4C4ikkAK7iIiCaTgLiKSQAruIiIJNDbsBgCMHz/etba2ht0MEZFY2bhx4/POuQl+z0UiuLe2trJhw4awmyEiEitmtj3fc0rLiIgkkIK7iEgCFQ3uZvZ9M9tlZr8dtm2pmT1hZo+a2U/NrHnYc51mtsXMesxsTq0aLiIi+ZXSc78JODVn2/3AMc65Y4EngU4AMzsaOAd4W+Y13zazhsBaKyIiJSka3J1z/wO8kLPt5865vZmH64FJme/PBG53zr3qnNsKbAFmBtheEREpQRDVMp8AlmW+b8EL9lk7M9tGMbP5wHyAyZMnB9AMEZHSdHX3snR1D0/39TOxuYkFc6bSMd03VMVWVQOqZvb/gL3AreW+1jl3g3Ou3TnXPmGCb5mmiEjgurp76Vyxmd6+fhzQ29dP54rNdHX3ht20QFUc3M3sn4G5wEfd0LrBvcCRw3ablNkmIhIJS1f30D8wOGJb/8AgS1f3hNSi2qgouJvZqcAXgTOcc3uGPXU3cI6ZHWhmbcAU4KHqmykiEoyn+/rL2h5XpZRC3gb8GphqZjvN7ALgOuANwP1mtsnM/g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z2IvCvzrm5aTwWmZ/5p5mHY4EfO+euNLPDgOXAZLxlrc92zr0QUjPryszejjfQfgDwR+B8Mn8zpOx4ZE74O4C3OOdeymyr6f8NBXcRkQRSWkZEJIEU3EVEEkjBXUQkgRTcRUQSSMFdRCSBFNxFRBJIwV1EJIH+P/CKU3R2NfoRAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x, y)\n",
    "\n",
    "# 针对每一个x，求出预测值\n",
    "y_pred = m * x + b\n",
    "\n",
    "# r为red\n",
    "plt.plot(x, y_pred, c='r')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
